package com.joshua.netdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.joshua.netdemo.EventBusDemo.EventBusAtyA;
import com.joshua.netdemo.JavaScriptDemo.HTMLAty;
import com.joshua.netdemo.JavaScriptDemo.JsBridgeAty;
import com.joshua.netdemo.MVPDemo.MvpAty;
import com.joshua.netdemo.OkHttpDemo.OkHttpAty;
import com.joshua.netdemo.RetrofitDemo.RetrofitAty;
import com.joshua.netdemo.WebViewDemo.WebViewActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> list;
    private ArrayList<Class> atyList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout
                .simple_list_item_1, list);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                Intent intent = new Intent(MainActivity.this, atyList.get(position));
                startActivity(intent);
            }
        });

        okhttpTest();
    }

    public static void okhttpTest() {
        String appKey = "afa6029173a8749cf06335d14fbaf265";
        String appSecret = "9f25fb3a576e";
        String nonce = "123456";
        String curTime = String.valueOf(new Date().getTime());
        String url = "https://api.netease.im/nimserver/user/create.action";

        OkHttpClient client = new OkHttpClient();

        RequestBody body = new FormBody.Builder()
                .add("accid", "136957890")
                .build();

        final Request request = new Request.Builder()
                .addHeader("AppKey", appKey)
                .addHeader("Nonce", nonce)
                .addHeader("CurTime", curTime)
                .addHeader("CheckSum", CheckSumBuilder.getCheckSum(appSecret, nonce,
                        curTime))
                .addHeader("Content-Type", "application/x-www-form-urlencoded;" +
                        "charset=utf-8")
                .post(body)
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.w("joshua", "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                Log.w("joshua", "onResponse: " + body.string());
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        atyList = new ArrayList<>();
        list.add("Retrofit Demo");
        atyList.add(RetrofitAty.class);
        list.add("OkHttp Demo");
        atyList.add(OkHttpAty.class);
        list.add("MVP Demo");
        atyList.add(MvpAty.class);
        list.add("JavaScript Demo");
        atyList.add(HTMLAty.class);
        list.add("JsBridge Demo");
        atyList.add(JsBridgeAty.class);
        list.add("EventBus");
        atyList.add(EventBusAtyA.class);
        list.add("WebView");
        atyList.add(WebViewActivity.class);
    }
}
